package com.gridmobi.xrtb.routers.bidding;

import com.alibaba.fastjson.JSONObject;
import com.gridmobi.xrtb.common.GeoMap;
import com.gridmobi.xrtb.domain.adn.OsE;
import com.gridmobi.xrtb.domain.adn.enums.DspSidEnum;
import com.gridmobi.xrtb.domain.dsp.JOffer;
import com.gridmobi.xrtb.domain.dsp.common.enums.ExchangesE;
import com.gridmobi.xrtb.handler.DeviceIdHandler;
import com.gridmobi.xrtb.handler.DeviceInfo;
import com.gridmobi.xrtb.handler.ImprOffersHandler;
import com.gridmobi.xrtb.model.rtb.ImpressionMacro;
import com.gridmobi.xrtb.saveclick.CounterClickHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/")
public class ClientImprController {
    private static final Logger errorlogger = LoggerFactory.getLogger("error");


    @Value("${client.script}")
    private int script;

    @GetMapping("geofiles")
    public void geofiles(HttpServletRequest request, HttpServletResponse response) throws Exception {


        response.getWriter().println(JSONObject.toJSONString(DeviceIdHandler.GEO_FILES));
        response.getWriter().flush();

    }

    @GetMapping("rtboffers")
    public void rtboffers(HttpServletRequest request, HttpServletResponse response) throws Exception {


        response.getWriter().println(JSONObject.toJSONString(ImprOffersHandler.GEO_OS_MAP));
        response.getWriter().flush();

    }

    @GetMapping("testoffer")
    public void testoffer(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String geo = request.getParameter("g");
        String t = request.getParameter("t");
        String geo2 = GeoMap.word3Map.get(geo);
        String osKey = OsE.getKeyByV(t);
        JOffer offer = ImprOffersHandler.getRandomOffer(geo2, osKey);
        if (offer == null) {
            response.getWriter().println("NONE");
            response.getWriter().flush();
        } else {
            response.getWriter().println(JSONObject.toJSONString(offer));
            response.getWriter().flush();
        }


    }

    @GetMapping("cimprpp")
    public void cimprpp(HttpServletRequest request, HttpServletResponse response) {

        String country = request.getParameter("t");
        String os = request.getParameter("o");
        errorlogger.error(country + os);
    }

    @GetMapping("cimpr1")
    public void cimpr1(HttpServletRequest request, HttpServletResponse response) {
        if(script==0){
            return;
        }
        String geo = request.getParameter("g");
        String t = request.getParameter("t");
        String sp = request.getParameter("sp");
        errorlogger.error(geo + t + sp);
        if (ExchangesE.ALGORIX.code.toString().equals(sp)) {
            redirect(DspSidEnum.ALGORX, request, response);

        } else {
            redirect(DspSidEnum.SMAATO, request, response);

        }

    }

    @GetMapping("cimpr")
    public void imp(HttpServletRequest request, HttpServletResponse response) {
        if(script==0){
            return;
        }
        String geo = request.getParameter("g");
        String t = request.getParameter("t");
        errorlogger.error(geo + t);

        redirect(DspSidEnum.SMAATO, request, response);
    }

    public void redirect(DspSidEnum SSP, HttpServletRequest request, HttpServletResponse response) {
        String geo3 = request.getParameter("g");
        String t = request.getParameter("t");
        try {
            String geo2 = GeoMap.word3Map.get(geo3);
            String osKey = OsE.getKeyByV(t);

            JOffer offer = ImprOffersHandler.getRandomOffer(geo2, osKey);
            if (offer != null) {
                String devid = DeviceIdHandler.takeDevid(geo3, osKey);
                DeviceInfo deviceInfo = DeviceIdHandler.getDevid(devid);
                String clickid = ImpressionMacro.getClickId(SSP.getSid(), offer, geo3, t, deviceInfo);
                String subid = ImpressionMacro.getSubid(offer, geo3, t);
                String url = ImpressionMacro.urlReplace(offer, geo3, t, offer.getTrackurl(), deviceInfo, subid, clickid);
                CounterClickHandler.countClick(offer.getId(), SSP.name(), false);

                errorlogger.error(url);
                response.sendRedirect(url);
            } else {
                try {
                    response.setStatus(204);
                    response.getWriter().println("");
                    response.getWriter().flush();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
            try {
                response.setStatus(204);
                response.getWriter().println("");
                response.getWriter().flush();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

    }

}
